import ResponseBody from './ResponseBody';
import BaseMethods from './BaseMethods';
import { ReturnCodeChzh, ReturnCodeEnum } from '@app/types/response';
import { Decrypt } from '@app/utils/aes/aes';

export default abstract class Api extends BaseMethods {
  /**
   * @name 接口调用成功
   * @description  方法调用位置要能抛出错误到最外层，如果不能，则获取不到返回值
   * @param msg 提示语
   * @param data 数据
   */
  protected success(
    data?: any,
    msg: string = '',
  ) {
    throw new ResponseBody({
      code: ReturnCodeEnum.Success,
      data,
      msg,
    });
  }
  /**
   * @name 接口调用失败
   * @description 用法及注意事项同 success
   * @param msg 提示语
   */
  protected fail(msg: string = '',) {
    throw new ResponseBody({
      code: ReturnCodeEnum.Fail,
      msg,
    });
  }

  /**
   * @name 传入参数错误
   * @description 用法及注意事项同 success
   * @param msg 提示语
   */
  protected payloadError(msg: string = '',) {
    throw new ResponseBody({
      code: ReturnCodeEnum.PayloadError,
      msg: msg || ReturnCodeChzh[ReturnCodeEnum.PayloadError],
    });
  }

  /**
    * @name 未登录
    * @description 用法及注意事项同 success
    */
  protected notLogin() {
    throw new ResponseBody({
      code: ReturnCodeEnum.NotLogin,
      msg: ReturnCodeChzh[ReturnCodeEnum.NotLogin],
    });
  }
  /**
   * @name 无权限
   * @description 用法及注意事项同 success
   */
  protected notAuth() {
    throw new ResponseBody({
      code: ReturnCodeEnum.NotAuthorized,
      msg: ReturnCodeChzh[ReturnCodeEnum.NotAuthorized],
    });
  }
  /**
   * @name 无权限
   * @description 用法及注意事项同 success
   */
  protected getUserId(): number {
    const head = this.getHeaders()
    if (!head.token) this.notLogin();
    const arr = Decrypt(head.token).split(' ')
    const id = parseInt(arr[1])
    if (isNaN(id) || id < 1) this.notLogin();
    return id
    // 
  }

}
